package cn.iune.web.erp.controllers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.justep.baas.Utils;

import cn.iune.config.SecurityService;
import cn.iune.erp.payment.IAccountPaymentService;
import cn.iune.erp.payment.IPaymentService;

@Controller
@RequestMapping(value="/invoice")
public class PaymentController {


	@Autowired
	private IAccountPaymentService accountPaymentService;
	
	@Autowired
	private IPaymentService paymentService;
	
	
	
	//付款单
	@RequestMapping(value="/payFKDJ/single",method=RequestMethod.GET)
	public ModelAndView payFKDJSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payFKDJ/single");
		mav.addObject("path","/invoice/payFKDJ/single");
		return mav;
	}
	
	//付款退回单
	@RequestMapping(value="/payFTDJ/single",method=RequestMethod.GET)
	public ModelAndView payFTDJSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payFTDJ/single");
		mav.addObject("path","/invoice/payFTDJ/single");
		return mav;
	}
	
	//付款核销单（作废）
	@RequestMapping(value="/payFKHX/single",method=RequestMethod.GET)
	public ModelAndView payFKHXSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payFKHX/single");
		mav.addObject("path","/invoice/payFKHX/single");
		return mav;
	}

	//收款单
	@RequestMapping(value="/recSKDJ/single",method=RequestMethod.GET)
	public ModelAndView recSKDJSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/recSKDJ/single");
		mav.addObject("path","/invoice/recSKDJ/single");
		return mav;
	}

	//退款单
	@RequestMapping(value="/recSTDJ/single",method=RequestMethod.GET)
	public ModelAndView recSTDJSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/recSTDJ/single");
		mav.addObject("path","/invoice/recSTDJ/single");
		return mav;
	}

	//收款核销
	@RequestMapping(value="/recSKHX/list",method=RequestMethod.GET)
	public ModelAndView recSKHX(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/recSKHX/list");
		return mav;
	}

	//付款核销
	@RequestMapping(value="/payFKHX/list",method=RequestMethod.GET)
	public ModelAndView payFKHX(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payFKHX/list");
		return mav;
	}

	//检查核销批号是否存在
	@RequestMapping(value={"/recSKHX/checkWriteOffNo","/payFKHX/checkWriteOffNo"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject checkWriteOffNo(@RequestBody JSONObject data){
		return paymentService.checkWriteOffNo(data);
	}

	//收款核销
	@RequestMapping(value={"/recSKHX/insert","/payFKHX/insert"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject recSKHXInsert(@RequestBody JSONObject data){
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");

		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
//		String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
//		String enterpriseName = SecurityService.getCurrentOgn().getsLongName();
		return paymentService.dkhx(data, cUsercode, cUsername, userId, employeeNo, employeeName);
	}

	//收款核销
	@RequestMapping(value={"/recSKHX/delete","/payFKHX/delete"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject recSKHXDelete(@RequestBody JSONObject data){
		return paymentService.fdkhx(data);
	}


	//收款--维护核销明细信息
	@RequestMapping(value="/recSKHXList/list",method=RequestMethod.GET)
	public ModelAndView recSKHXList(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/recSKHXList/list");
		//mav.addObject("path","/invoice/recSKHXList/list");
		return mav;
	}

	//收款--单据核销明细表
	@RequestMapping(value="/recSKHXQuery/list",method=RequestMethod.GET)
	public ModelAndView recSKHXQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/recSKHXQuery/list");
		return mav;
	}

	//付款--维护核销明细信息
	@RequestMapping(value="/payFKHXList/list",method=RequestMethod.GET)
	public ModelAndView payFKHXList(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payFKHXList/list");
		mav.addObject("path","/invoice/payFKHXList/list");
		return mav;
	}

	//付款--单据核销明细表
	@RequestMapping(value="/payFKHXQuery/list",method=RequestMethod.GET)
	public ModelAndView payFKHXQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/payFKHXQuery/list");
		return mav;
	}
	
	//选择预收预付
	@RequestMapping(value="/paymentSelect/selectMulti",method=RequestMethod.GET)
	public ModelAndView paymentSelectMultip(){
		ModelAndView mav = new ModelAndView("/appcenter/invoice/paymentSelect/selectMulti");
		return mav;
	}


	@RequestMapping(value={
			"/payFKDJ/single/query"
			,"/payFTDJ/single/query"
//			,"/payFKHX/single/query"
			,"/recSKHX/single/query"
			,"/recSKDJ/single/query"
			,"/recSTDJ/single/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payFKDJQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return accountPaymentService.queryData(data);
	}

	@RequestMapping(value={
			"/payFKDJ/single/save"
			,"/payFTDJ/single/save"
//			,"/payFKHX/single/save"
			,"/recSKHX/single/save"
			,"/recSKDJ/single/save"
			,"/recSTDJ/single/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payFKDJSave(@RequestBody JSONObject object) {
		return accountPaymentService.saveData(object);
	}

	@RequestMapping(value={
			"/payFKDJ/single/delete"
			,"/payFTDJ/single/delete"
//			,"/payFKHX/single/delete"
			,"/recSKHX/single/delete"
			,"/recSKDJ/single/delete"
			,"/recSTDJ/single/delete"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payFKDJDelete(@RequestBody JSONObject data) {
		return accountPaymentService.deleteData(data);
	}

	@RequestMapping(value={
			"/payFKDJ/single/create"
			,"/payFTDJ/single/create"
//			,"/payFKHX/single/create"
			,"/recSKHX/single/create"
			,"/recSKDJ/single/create"
			,"/recSTDJ/single/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payFKDJCreate(@RequestBody JSONObject data, HttpServletRequest request) {
//		data = SecurityService.translateFun(data);
//		return accountPaymentService.newData(data);
		System.out.println("controller.data-->>"+data.toJSONString());
		JSONObject params = JSONObject.parseObject(data.toJSONString());
		//增加登录信息
		//缺省值
		JSONArray defaultValues = params.getJSONArray("defaultValues");
		//循环缺省值（多行）
		for(int i=0;i<defaultValues.size();i++) {
			JSONObject rowValue = defaultValues.getJSONObject(i);
			//循环列
			//循环缺省值（单行）
			for(String col: rowValue.keySet()) {
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
				if(":getCurrentDate()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDate());
				}
				if(":getCurrentPsmFID()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPsm().getsFID());
				}
				if(":getCurrentPsmFName()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPsm().getsFName());
				}
				if(":getCurrentDateTime()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDateTime());
				}
				if(":getCurrentPersonCode()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPerson().getString("cUsercode"));
				}
				if(":getCurrentPersonName()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPerson().getString("cUsername"));
				}
				if(":getCurrentPersonID()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPerson().getString("userId"));
				}
				if(":getCurrentOgnFID()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentOgn().getsFID());
				}
				if(":getCurrentOgnLongName()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentOgn().getsLongName());
				}
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
			}
			
		}
		return accountPaymentService.newData(params);
	}

	
	@RequestMapping(value={
			"/payFKDJ/single/createDetail"
			,"/payFTDJ/single/createDetail"
//			,"/payFKHX/single/createDetail"
			,"/recSKHX/single/createDetail"
			,"/recSKDJ/single/createDetail"
			,"/recSTDJ/single/createDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payFKDJCreateDetail(@RequestBody JSONObject data) {
		return accountPaymentService.newDataDetail(data);
	}
	


	@RequestMapping(value={
			"/payFKDJ/single/queryDetail"
			,"/payFTDJ/single/queryDetail"
//			,"/payFKHX/single/queryDetail"
			,"/recSKHX/single/queryDetail"
			,"/recSKDJ/single/queryDetail"
			,"/recSTDJ/single/queryDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject payFKDJQueryDetail(@RequestBody JSONObject data) {
		return accountPaymentService.queryDataDetail(data);
	}


	

	
	//////////////////////////////////////////////////////////////////////////////////////
	@RequestMapping(value={
			"/payFKDJ/single/confirm"
			,"/payFTDJ/single/confirm"
//			,"/payFKHX/single/confirm"
			,"/recSKHX/single/confirm"
			,"/recSKDJ/single/confirm"
			,"/recSTDJ/single/confirm"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payFKDJConfirm(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		return paymentService.accountPaymentConfirm(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}

	@RequestMapping(value={
			"/payFKDJ/single/active"
			,"/payFTDJ/single/active"
//			,"/payFKHX/single/active"
			,"/recSKHX/single/active"
			,"/recSKDJ/single/active"
			,"/recSTDJ/single/active"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payFKDJActive(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return paymentService.accountPaymentActive(id);
	}
	
	
	@RequestMapping(value={
			"/payFKDJ/single/agreeIt"
			,"/payFTDJ/single/agreeIt"
//			,"/payFKHX/single/agreeIt"
			,"/recSKHX/single/agreeIt"
			,"/recSKDJ/single/agreeIt"
			,"/recSTDJ/single/agreeIt"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payFKDJAgreeIt(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		return paymentService.accountPaymentAgreeIt(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}

	@RequestMapping(value={
			"/payFKDJ/single/unAgree"
			,"/payFTDJ/single/unAgree"
//			,"/payFKHX/single/unAgree"
			,"/recSKHX/single/unAgree"
			,"/recSKDJ/single/unAgree"
			,"/recSTDJ/single/unAgree"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payFKDJUnAgree(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return paymentService.accountPaymentUnAgree(id);
	}
	@RequestMapping(value={
			"/payFKDJ/single/close"
			,"/payFTDJ/single/close"
//			,"/payFKHX/single/close"
			,"/recSKHX/single/close"
			,"/recSKDJ/single/close"
			,"/recSTDJ/single/close"
			},method=RequestMethod.POST)
	@ResponseBody
	public void payFKDJClose(@RequestBody JSONObject data) {
		String id = data.getString("id");
		paymentService.accountPaymentClose(id);
	}

	@RequestMapping(value={
			"/payFKDJ/single/open"
			,"/payFTDJ/single/open"
//			,"/payFKHX/single/open"
			,"/recSKHX/single/open"
			,"/recSKDJ/single/open"
			,"/recSTDJ/single/open"
			},method=RequestMethod.POST)
	@ResponseBody
	public void payFKDJOpen(@RequestBody JSONObject data) {
		String id = data.getString("id");
		paymentService.accountPaymentOpen(id);
	}

	@RequestMapping(value={
			"/payFKDJ/single/printPDF"
			,"/payFTDJ/single/printPDF"
			,"/recSKDJ/single/printPDF"
			,"/recSTDJ/single/printPDF"
			},method=RequestMethod.GET)
	@ResponseBody
	public void payFKDJPrintPDF(@RequestParam(value="id",required=true) String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//invoiceService.accountPaymentPrintPDF(id, response);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");
//	    PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
	    PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
	    paymentService.accountPaymentPrintPDF(id,doc);
		//关闭doc
	    doc.close();
	}

	//////////////////////////////////////////////////////////////////////////////////////

	
	
	

	@RequestMapping(value={"/accountPayment/query"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountPaymentQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return accountPaymentService.queryData(data);
	}
	

	@RequestMapping(value={"/accountPaymentDetail/query"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject accountPaymentDetailQuery(@RequestBody JSONObject data) {
//		data = SecurityService.addDataAccessPerm(data);
		return accountPaymentService.queryDataDetail(data);
	}


	//今日收款金额
	@RequestMapping(value="/recSKDJ/getRecAmountToday",method=RequestMethod.GET)
	@ResponseBody
	public JSONObject getRecAmountToday() {
		JSONObject json = new JSONObject();
		
		if(SecurityService.getCurrentOgn()==null){
			//实际上下面这一段不会被执行，因为shiro会首先拦截掉未登录的请求，重定向到未授权页面。
			json.put("code", 403);
			json.put("msg", "未登录");
			return json;
			
		}else {
			String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
			List<Object> params = new ArrayList<Object>();
			params.add(enterpriseNo);
			params.add(Utils.getCurrentDate());
			BigDecimal count = accountPaymentService.getRecAmountToday(params);
			if(count==null) count = BigDecimal.ZERO;
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", count);
			return json;
		}
	}

	//今日付款金额
	@RequestMapping(value="/payFKDJ/getPayAmountToday",method=RequestMethod.GET)
	@ResponseBody
	public JSONObject getPayAmountToday() {
		JSONObject json = new JSONObject();
		
		if(SecurityService.getCurrentOgn()==null){
			//实际上下面这一段不会被执行，因为shiro会首先拦截掉未登录的请求，重定向到未授权页面。
			json.put("code", 403);
			json.put("msg", "未登录");
			return json;
			
		}else {
			String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
			List<Object> params = new ArrayList<Object>();
			params.add(enterpriseNo);
			params.add(Utils.getCurrentDate());
			BigDecimal count = accountPaymentService.getPayAmountToday(params);
			if(count==null) count = BigDecimal.ZERO;
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", count);
			return json;
		}
	}


	//现结
	//////////////////////////////////////////////////////////////////////////////
//	@RequestMapping(value="/cashSettle/single",method=RequestMethod.GET)
//	public ModelAndView cashSettleView(){
//		ModelAndView mav = new ModelAndView("/appcenter/invoice/cashSettle/single");
//		return mav;
//	}
//
//	//现结
//	@RequestMapping(value = "/receiveXSFP/single/pay", method = RequestMethod.POST)
//	@ResponseBody
//	public JSONObject receiveXSFPPay(@RequestBody JSONObject params, HttpServletRequest request){
//		//增加人员信息
//		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
//		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
//		String userId = SecurityService.getCurrentPerson().getString("userId");
//		
//		String employeeNo = null;
//		String employeeName = null;
//		return paymentService.cashSettleSk(params, cUsercode, cUsername, userId, employeeNo, employeeName);
//	}

	
	//刷新付款状态
	@RequestMapping(value = "/receiveXSFP/single/payRefresh", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject receiveXSFPPayRefresh(@RequestBody JSONObject params) {
//		System.out.println(params.toJSONString());
		//增加人员信息
//		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
//		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
//		String userId = SecurityService.getCurrentPerson().getString("userId");
//		String employeeNo = null;
//		String employeeName = null;
		return null;
		
	}
	
	//关闭交易
//	@RequestMapping(value = "/receiveXSFP/single/payClose", method = RequestMethod.POST)
//	@ResponseBody
//	public JSONObject receiveXSFPPayClose(@RequestBody JSONObject params) throws AlipayApiException{
//		return invoiceService.aliTradeClose(params);
//	}
	
	//撤销交易
	@RequestMapping(value = "/receiveXSFP/single/payCancel", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject receiveXSFPPayCancel(@RequestBody JSONObject params) {
		return null;
	}

	
	
	
	
	
	
}
